package com.kanshu.ksgb.fastread.doudou.common.business.pay;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.presenter.AdPresenter;
import com.kanshu.ksgb.fastread.doudou.common.util.StorageUtils;
import com.kanshu.ksgb.fastread.doudou.common.business.manager.MMKVDefaultManager;
import com.kanshu.ksgb.fastread.doudou.common.business.pay.bean.OrderRequestParams;

import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 支付类型选择对话框
 * Created by wcy on 2017\8\1 0001.
 */
public class PayTypeSelectDialog extends Dialog {
    private int mTotalFee;
    private int mCoin;
    private int mDay;
    private int mPayType = Constants.PayType.TYPE_ALI;
    //0 正常支付， 1包天， 2免广告
    private int mBusinessType;
    private Activity mActivity;

    public PayTypeSelectDialog(Context context, int totalFee, int coin) {
        super(context, R.style.dialog_style);
        mTotalFee = totalFee;
        mCoin = coin;
        mActivity = (Activity) context;
        init();
    }

    public PayTypeSelectDialog(Context context, int totalFee, int coin, int day, int businessType) {
        super(context, R.style.dialog_style);
        mTotalFee = totalFee;
        mCoin = coin;
        mDay = day;
        mBusinessType = businessType;
        mActivity = (Activity) context;
        init();
    }


    private void init() {
        setContentView(R.layout.dialog_pay_select_layout);
        ButterKnife.bind(this);
/*        mWxCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mAliCheckbox.setChecked(!isChecked);
                if (mWxCheckbox.isChecked()) {
                    mPayType = Constants.PayType.TYPE_WX;
                }
            }
        });
        mAliCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mWxCheckbox.setChecked(!isChecked);
                if (isChecked) {
                    mPayType = Constants.PayType.TYPE_ALI;
                }
            }
        });*/
    }

    @OnClick({R.id.wx_container, R.id.ali_container})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.wx_container:
                mPayType = Constants.PayType.TYPE_WX;
                dismiss();
                pay();
                break;
            case R.id.ali_container:
                mPayType = Constants.PayType.TYPE_ALI;
                dismiss();
                pay();
                break;
            default:
                break;
        }
    }

    private void pay() {
        if (mCoin == 0) {
            String pvuvStr = null;
            switch (mDay) {
                case 1:
                    pvuvStr = "vip_free_ad_1";
                    break;
                case 7:
                    pvuvStr = "vip_free_ad_7";
                    break;
                case 30:
                    pvuvStr = "vip_free_ad_30";
                    break;
                case 365:
                    pvuvStr = "vip_free_ad_365";
                    break;
                case -1:
                    pvuvStr = "vip_free_ad_0";
                    break;
                default:
                    break;
            }
            if (!TextUtils.isEmpty(pvuvStr)) {
                AdPresenter.Companion.pvuvStatics(pvuvStr, "");
            }
        }

        AdPresenter.Companion.pvuvStatics(mPayType == Constants.PayType.TYPE_ALI ? "pay_zhifubao" : "pay_wechat", "");

        StringBuilder sb = new StringBuilder();
        sb.append(mCoin).append("#")
                .append(mTotalFee).append("#")
                .append(mPayType)
                .append("#").append(mBusinessType);
        MMKVDefaultManager.getInstance().savePayInfo(sb.toString());
        OrderRequestParams params = new OrderRequestParams();
        params.coin = mCoin;
        params.pay_type = mPayType;
        params.total_fee = mTotalFee;
        params.day = mDay;
        params.business_type = mBusinessType;
        params.book_id = StorageUtils.getPreference(getContext(), Constants.SP_NAME, "charge_book_id", "");
        if (mPayType == Constants.PayType.TYPE_ALI) {
            AliPayHelper helper = new AliPayHelper(mActivity);
            helper.payAction(params);
        } else if (mPayType == Constants.PayType.TYPE_WX) {
            WxPayHelper helper = new WxPayHelper(mActivity);
            helper.payAction(params);
        }
        dismiss();
    }

    /**
     * 广告小说支付
     *
     * @param totalFee
     * @param coin     pay + give
     */
    public static PayTypeSelectDialog show(Activity activity, int totalFee, int coin) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }
        PayTypeSelectDialog dlg = new PayTypeSelectDialog(activity, totalFee, coin);
        dlg.show();
        return dlg;
    }

    /**
     * 广告小说支付
     *
     * @param totalFee
     * @param coin         免广告传0
     * @param day          免广告天数
     * @param businessType 1包天,2 去广告
     */
    public static PayTypeSelectDialog show(Activity activity, int totalFee, int coin, int day, int businessType) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }
        PayTypeSelectDialog dlg = new PayTypeSelectDialog(activity, totalFee, coin, day, businessType);
        dlg.show();
        return dlg;
    }

}
